/*
This file is part of AI Agent Feed.

"AI Agent Feed" is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

"AI Agent Feed" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with "AI Agent Feed".  If not, see <http://www.gnu.org/licenses/>.
 */
package com.feed.domain;

import java.awt.Image;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;

public class Item implements Serializable {
    
    public static final Comparator<Item> PUB_DATE_ORDER = new Comparator<Item>() {
        public int compare(Item item1, Item item2) {
        	if ((item1.getPubDate() == null)&& (item2.getPubDate() == null)) {
                return 0;
        	}else if ((item1.getPubDate() == null) || (item2.getPubDate() == null)) {
                return -1;
            }else if (item1.getPubDate().isBefore(item2.getPubDate())) {
                return 1;
            } else if (item1.getPubDate().equals(item2.getPubDate())) {
                return 0;
            }
            return -1;
        }
    };
    

    private final FeedProvider source;
    private String guid;
    private String originalGuid;
    private String url;
    private String title;
    private String content;
    private String summary;
    private String region;
    private String copyright;
    private List<String> imageGuids;
    private FeedCategory category;
    private DateTime pubDate;
    private Image	image;

	public Item(FeedProvider source) {
        this.source = source;
        imageGuids = new ArrayList<String>();
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String description) {
        this.content = description;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getGuid() {
        return guid;
    }

    public void setGuid(String guid) {
        this.guid = guid;
    }

    public DateTime getPubDate() {
        return pubDate;
    }

    public void setPubDate(DateTime pubDate) {
        this.pubDate = pubDate;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public FeedCategory getCategory() {
        return category;
    }

    public void setCategory(FeedCategory category) {
        this.category = category;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Image getImage() {
    	return image;
    }

	public void setImage(Image image) {
    	this.image = image;
    }

    /**
     * @Deprecated Use getImage instead. 
     */
    public List<String> getImageGuids() {
        return Collections.unmodifiableList(imageGuids);
    }

    public void addImageGuid(String imageGuid) {
        imageGuids.add(imageGuid);
    }
    
    public void clearImageGuids() {
        imageGuids.clear();
    }
    
    public String getOriginalGuid() {
        return originalGuid;
    }
    
    public void setOriginalGuid(String originalGuid) {
        this.originalGuid = originalGuid;
    }
    
    public FeedProvider getSource() {
        return source;
    }

	public String getCopyright() {
		return copyright;
	}

	public void setCopyright(String copyright) {
		this.copyright = copyright;
	}
	
	@Override
    public String toString() {
        StringBuilder buffer = new StringBuilder();
        buffer.append(getClass().getSimpleName());
        buffer.append("[");
        buffer.append("guid=").append(guid);
        buffer.append(",category=").append(category);
        buffer.append(",title=").append(title);
        buffer.append(",pubDate=").append(pubDate == null ? null : ISODateTimeFormat.basicDateTimeNoMillis().print(pubDate));
        buffer.append(",originalGuid=").append(originalGuid);
        buffer.append("]");
        return buffer.toString();
    }

    @Override
    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        result = PRIME * result + ((category == null) ? 0 : category.hashCode());
        result = PRIME * result + ((content == null) ? 0 : content.hashCode());
        result = PRIME * result + ((guid == null) ? 0 : guid.hashCode());
        result = PRIME * result + ((url == null) ? 0 : url.hashCode());
        result = PRIME * result + ((summary == null) ? 0 : summary.hashCode());
        result = PRIME * result + ((title == null) ? 0 : title.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        final Item other = (Item) obj;
        if (category == null) {
            if (other.category != null) return false;
        } else if (!category.equals(other.category)) return false;
        if (content == null) {
            if (other.content != null) return false;
        } else if (!content.equals(other.content)) return false;
        if (guid == null) {
            if (other.guid != null) return false;
        } else if (!guid.equals(other.guid)) return false;
        if (url == null) {
            if (other.url != null) return false;
        } else if (!url.equals(other.url)) return false;
        if (summary == null) {
            if (other.summary != null) return false;
        } else if (!summary.equals(other.summary)) return false;
        if (title == null) {
            if (other.title != null) return false;
        } else if (!title.equals(other.title)) return false;
        return true;
    }
}
